package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogMapper catalogMapper;

    public PageInfo queryForList(QueryObject qo){
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderby());
        List<StrategyCatalog> list = catalogMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public void saveOrUpdate(StrategyCatalog catalog) {
        if (catalog.getId() != null) {
            catalogMapper.updateByPrimaryKey(catalog);
        }else {
            if (catalog.getSequence() == null) {
                int maxSequence = catalogMapper.selectMaxSequence(catalog.getStrategy().getId());
                    catalog.setSequence(maxSequence + 1);
            }
            catalogMapper.insert(catalog);
        }
    }

    public List<StrategyCatalog> listByStrategyId(Long strategyId) {
        return catalogMapper.listByStrategyId(strategyId);
    }
}
